# @Time : 2020/8/31 10:59
# @Author : Fioman 
# @Phone : 13149920693
"""
使用轮廓近似算法,来搞定这个东西.
"""
import cv2 as cv
import numpy as np
import imutils

image = cv.imread("pic/22.png")
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

cnts = cv.findContours(gray.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for c in cnts:
    clone = image.copy()
    perimeter = cv.arcLength(c, True)
    approx = cv.approxPolyDP(c, 0.01 * perimeter, True)
    print(len(approx))
    cv.drawContours(clone,[c],-1,(0,255,0),5)
    cv.imshow("Clone",clone)
    cv.waitKey(0)
    if len(approx) == 4:
        cv.drawContours(image,[c],-1,(0,255,255),2)
        x,y,w,h = cv.boundingRect(approx)
        cv.putText(image,"Rectangle",(x,y-10),cv.FONT_HERSHEY_SIMPLEX,0.5,(0,255,255),2)

cv.imshow("Image",image)
cv.waitKey(0)

